<?php
class Admin_VentesController extends My_Controller_Admin_Action
{
	protected $ventesModel = '';

	public function _init()
	{
		$this->ventesModel = new Admin_Model_Ventes();
	}

	public function indexAction()
	{
		$rowPerPage = $this->_getParam('rowperpage', 20);
		$currentPage = 1;
		$page = $this->_getParam('page', 1);
		if(!empty($page)) {
			$currentPage = $page;
		}

		$ventesModel = new Admin_Model_Ventes();
		$venteCollection = $ventesModel->getListVentes();

		$rows = $this->ventesModel->getListVentes();
		$paginator = Zend_Paginator::factory($rows);
		$paginator->setItemCountPerPage($rowPerPage);
		$paginator->setCurrentPageNumber($currentPage);

		$this->view->page = $page;
		$this->view->paginator = $paginator;
		$this->view->venteCollection = $venteCollection;
	}

	public function ventesAction()
	{
	   $id = $this->_getParam('vente', false);
	   if (!$id) {
	       $this->getHelper('flashMessenger')->setnamespace('error')->addMessage('Ventes pas exister');
	       $this->_redirect('admin/ventes/choose-ventes');
	   }
	}

	public function editAction()
	{
	    $id = $this->_getParam('id', null);
	    $data = array();
	    if ($id) {
	        $data = $this->ventesModel->getVentes($id);
	    }
	    $this->view->object = new My_Object($data);
	}

	public function saveAction()
	{
	    $id = $this->_getParam('id', null);
	    $data = $this->_getParam('data', array());
	    if ($data['dtvente']) {
	        $data['dtvente'] = Admin_Model_Date::fomatDbDate($data['dtvente']);
	    }
	    if ($data['cat_premier']) {
	        $data['cat_premier'] = implode(',', $data['cat_premier']);
	    }
	    if ($data['cat_second']) {
	        $data['cat_second'] = implode(',', $data['cat_second']);
	    }
	    if ($data['cat_troisieme']) {
	        $data['cat_troisieme'] = implode(',', $data['cat_troisieme']);
	    }

	    $this->ventesModel->save($data, $id);
	    $this->_redirect('admin/ventes');
	}

	public function ajoutAction()
	{
		$this->_forward('edit');
	}

	public function deleteVentesAction()
	{
		$id = $this->_getParam('id');
		$page = $this->_getParam('page', 1);
		$ventesModel = new Admin_Model_Ventes();
		$ventesModel->removeVentes($id);

		$param = '';
		if($page > 1) $param = 'index/page/' . $page;
		$this->_redirect('admin/ventes/' . $param);
		exit;
	}
}

